﻿using System.IO;
using System.Linq;
using Moq;
using PluginFramework.Packages;
using Xunit;
using System;
using System.Diagnostics;

namespace PluginTests.PackageTests
{
	public class PackageManagerTests
	{
		private Stopwatch _t = new Stopwatch();
		private DirectoryInfo _cachedir;
		private PackageManager _pm;

		public PackageManagerTests()
		{
			Setup.SetPackageManagerPath();
			var settings = new Mock<IPackageSettingsProvider>();
			settings.Setup(s => s.ReadPackageList()).Returns(new[] {"Plugin2"});
			
			_pm = new PackageManager(settings.Object);
			try
			{
				_pm.CopyToCache();
			}
			catch
			{
				Console.WriteLine("CopyCache ignored");
			}
			_cachedir = new DirectoryInfo(PackageManager.CachePluginDirectoryPath);
		}

		
		[Fact]
		public void all_set_packages_are_copied()
		{
			var files = _cachedir.EnumerateFiles().ToArray();
			Assert.Equal(2,files.Count());
			Assert.True(files.Any(f=>f.Name=="Plugin2.dll"));			
			Assert.True(files.Any(f=>f.Name=="Common.dll"));			
		}

		[Fact]
		public void test_package_has_loaded_two_assemblies()
		{
			_pm.LoadPackages();
			Assert.Equal(2,_pm.Assemblies.Count());
			Assert.True(_pm.Assemblies.Any(a=>a.FullName.Contains("Plugin2")));
			Assert.True(_pm.Assemblies.Any(a=>a.FullName.Contains("Common")));
		}

		//[Fact()]
		public void all()
		{
			_pm.LoadPackages(asm=>
			         	{
							foreach (var tp in asm.GetExportedTypes())
							{
								Write("Assembly:{0} Type: {1}",asm.FullName,tp.FullName);

							}
			         	});

			
		}

		private void Write(string format, params object[] param)
		{
			Console.WriteLine(format, param);
		}
	}
}